﻿/**
 * @file button_process.c
 * @brief this is a brief description
 * @details this is the detail description.
 * @author fulong 
 * @data 8/21/2014
 * @version 1.0
 * @par Copyright (c):
 * @par First build
 *     version: fulong 
 *     data: 8/21/2014
 */
#include "XT14012_button_process.h"

#include "../../../../app/menu/menu_process.h"
#include "../../../../app/menu/menu_common.h"
#include "../../../../hal/e2prom.h"
#include "../../../../dev/uart.h"
#include "../../../../hal/action/action.h"

#ifdef MENU_ENABLE
    #ifdef __XT14012__
    #define PARAM_ITEM_LEN 8
const uint8 *param_item[] = { "参数1", "参数2", "参数3", "参数4", "参数5", "参数6", "参数7", "参数8" };
    #define SELECT_PROGRAM_PASSWD_HIGH 0
    #define SELECT_PROGRAM_PASSWD_LOW 506
    #define PARAM_FLASH_LEFT_EDGE 16
    #define PARAM_FLASH_RIGHT_EDGE 23
    #define PARAM_FLASH_WIDGET_COUNTER4_ONE_LINE 4
uint8 param_counter = 0;
uint8 param_num;
extern uint8 param[PARAM_MAX];
void enter_passwd_button(void)
{
    change_menu(2);
}
void menu_passwd_button_up(void)
{
    ctrllable_widget_var_up(get_ctrllable_widget_pointer(), 10);
}
void menu_passwd_button_down(void)
{
    ctrllable_widget_var_down(get_ctrllable_widget_pointer(), 10);
}
void menu_passwd_button_left_select(void)
{
    uint8 ctrllable_num;
    ctrllable_num = get_left_ctrllable_widget();
    if (ctrllable_num != GET_CTRLLABLE_WRONG)
    {
        clr_ctrllable_widget_flash(get_ctrllable_widget_pointer());
        set_ctrllable_widget_pointer(ctrllable_num);
        set_ctrllable_widget_flash(get_ctrllable_widget_pointer());
    }
}
void menu_passwd_button_right_select(void)
{
    uint8 ctrllable_num;
    ctrllable_num = get_right_ctrllable_widget();
    if (ctrllable_num != GET_CTRLLABLE_WRONG)
    {
        clr_ctrllable_widget_flash(get_ctrllable_widget_pointer());
        set_ctrllable_widget_pointer(ctrllable_num);
        set_ctrllable_widget_flash(get_ctrllable_widget_pointer());
    }
}
void menu_passwd_button_sure(void)
{
    uint16 passwd_high;
    uint16 passwd_low;
    passwd_high = get_ctrllable_widget_var_value(3) * 100 + get_ctrllable_widget_var_value(4) * 10 + get_ctrllable_widget_var_value(5);
    passwd_low = get_ctrllable_widget_var_value(6) * 100 + get_ctrllable_widget_var_value(7) * 10 + get_ctrllable_widget_var_value(8);
    set_ctrllable_widget_var_value(3, 0);
    set_ctrllable_widget_var_value(4, 0);
    set_ctrllable_widget_var_value(5, 0);
    set_ctrllable_widget_var_value(6, 0);
    set_ctrllable_widget_var_value(7, 0);
    set_ctrllable_widget_var_value(8, 0);
    if (passwd_high == SELECT_PROGRAM_PASSWD_HIGH && passwd_low == SELECT_PROGRAM_PASSWD_LOW)
    {
        change_menu(2);
        clr_ctrllable_widget_flash(get_ctrllable_widget_pointer());
        set_ctrllable_widget_pointer(PARAM_FLASH_LEFT_EDGE + PARAM_FLASH_WIDGET_COUNTER4_ONE_LINE - 1);
        set_ctrllable_widget_flash(get_ctrllable_widget_pointer());
        set_ctrllable_widget_txt_value(14, param_item[param_counter]);
        set_ctrllable_widget_txt_value(15, param_item[param_counter + 1]);
        param_num = 0;
    }
}
void menu_param_button_next_page(void)
{
    uint8 i;
    uint16 param_save;
    param_counter += 2;
    param_counter %= PARAM_ITEM_LEN;
    param_save =  get_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE) * 1000 + get_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE + 1) * 100 + get_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE + 2) + get_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE + 3);
    set_param(param_num, param_save);
    param_save =  get_ctrllable_widget_var_value(PARAM_FLASH_RIGHT_EDGE - 3) * 1000 + get_ctrllable_widget_var_value(PARAM_FLASH_RIGHT_EDGE - 2) * 100 + get_ctrllable_widget_var_value(PARAM_FLASH_RIGHT_EDGE - 1) + get_ctrllable_widget_var_value(PARAM_FLASH_RIGHT_EDGE);
    set_param(param_num + 1, param_save);
    for (i = 0; i < PARAM_FLASH_WIDGET_COUNTER4_ONE_LINE * 2; i++)
    {
        set_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE + i, 0);
    }
    param_num += 2;
    param_num %= PARAM_ITEM_LEN;
    set_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE, read_action_param(param_num) / 1000 % 10);
    set_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE + 1, read_action_param(param_num) / 100 % 10);
    set_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE + 2, read_action_param(param_num) / 10 % 10);
    set_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE + 3, read_action_param(param_num) % 10);
    set_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE + 4, read_action_param(param_num + 1) / 1000 % 10);
    set_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE + 5, read_action_param(param_num + 1) / 100 % 10);
    set_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE + 6, read_action_param(param_num + 1) / 10 % 10);
    set_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE + 7, read_action_param(param_num + 1) % 10);
    clr_ctrllable_widget_flash(get_ctrllable_widget_pointer());
    set_ctrllable_widget_txt_value(14, param_item[param_counter]);
    set_ctrllable_widget_txt_value(15, param_item[param_counter + 1]);
    set_ctrllable_widget_pointer(PARAM_FLASH_LEFT_EDGE + PARAM_FLASH_WIDGET_COUNTER4_ONE_LINE - 1);
    set_ctrllable_widget_flash(get_ctrllable_widget_pointer());
}
void menu_param_button_sure(void)
{
    uint8 i = get_ctrllable_widget_pointer();
    uint16 param_save;
    clr_ctrllable_widget_flash(get_ctrllable_widget_pointer());
    if (i >= PARAM_FLASH_LEFT_EDGE && i < (PARAM_FLASH_LEFT_EDGE + PARAM_FLASH_WIDGET_COUNTER4_ONE_LINE))
    {
        param_save =  get_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE) * 1000 + get_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE + 1) * 100 + get_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE + 2) + get_ctrllable_widget_var_value(PARAM_FLASH_LEFT_EDGE + 3);
        set_param(param_num, param_save);
        set_ctrllable_widget_pointer(PARAM_FLASH_RIGHT_EDGE);
    } else
    {
        param_save =  get_ctrllable_widget_var_value(PARAM_FLASH_RIGHT_EDGE - 3) * 1000 + get_ctrllable_widget_var_value(PARAM_FLASH_RIGHT_EDGE - 2) * 100 + get_ctrllable_widget_var_value(PARAM_FLASH_RIGHT_EDGE - 1) + get_ctrllable_widget_var_value(PARAM_FLASH_RIGHT_EDGE);
        set_param(param_num + 1, param_save);
        set_ctrllable_widget_pointer(PARAM_FLASH_LEFT_EDGE + PARAM_FLASH_WIDGET_COUNTER4_ONE_LINE - 1);
    }
    set_ctrllable_widget_flash(get_ctrllable_widget_pointer());
}
void menu_param_button_left(void)
{
    uint8 ctrllable_num;
    ctrllable_num = get_ctrllable_widget_pointer();
    if (ctrllable_num >= PARAM_FLASH_LEFT_EDGE && ctrllable_num < (PARAM_FLASH_LEFT_EDGE + PARAM_FLASH_WIDGET_COUNTER4_ONE_LINE))
    {
        if (ctrllable_num == PARAM_FLASH_LEFT_EDGE)
        {
            ctrllable_num = PARAM_FLASH_LEFT_EDGE + PARAM_FLASH_WIDGET_COUNTER4_ONE_LINE;
            clr_ctrllable_widget_flash(get_ctrllable_widget_pointer());
            set_ctrllable_widget_pointer(ctrllable_num);
        }
        ctrllable_num = get_left_ctrllable_widget();
        clr_ctrllable_widget_flash(get_ctrllable_widget_pointer());
        set_ctrllable_widget_pointer(ctrllable_num);
        set_ctrllable_widget_flash(get_ctrllable_widget_pointer());
    } else if (ctrllable_num > (PARAM_FLASH_RIGHT_EDGE - PARAM_FLASH_WIDGET_COUNTER4_ONE_LINE) && ctrllable_num <= PARAM_FLASH_RIGHT_EDGE)
    {
        if (ctrllable_num ==  (PARAM_FLASH_RIGHT_EDGE - PARAM_FLASH_WIDGET_COUNTER4_ONE_LINE + 1))
        {
            ctrllable_num = PARAM_FLASH_RIGHT_EDGE;
            clr_ctrllable_widget_flash(get_ctrllable_widget_pointer());
            set_ctrllable_widget_pointer(ctrllable_num);
        } else
        {
            ctrllable_num = get_left_ctrllable_widget();
            clr_ctrllable_widget_flash(get_ctrllable_widget_pointer());
            set_ctrllable_widget_pointer(ctrllable_num);
        }
        set_ctrllable_widget_flash(get_ctrllable_widget_pointer());
    }
}
void menu_param_button_right(void)
{
    uint8 ctrllable_num;
    ctrllable_num = get_ctrllable_widget_pointer();
    if (ctrllable_num >= PARAM_FLASH_LEFT_EDGE && ctrllable_num < (PARAM_FLASH_LEFT_EDGE + PARAM_FLASH_WIDGET_COUNTER4_ONE_LINE))
    {
        if (ctrllable_num == (PARAM_FLASH_LEFT_EDGE + PARAM_FLASH_WIDGET_COUNTER4_ONE_LINE - 1))
        {
            ctrllable_num = PARAM_FLASH_LEFT_EDGE - 1;
            clr_ctrllable_widget_flash(get_ctrllable_widget_pointer());
            set_ctrllable_widget_pointer(ctrllable_num);
        }
        ctrllable_num = get_right_ctrllable_widget();
        clr_ctrllable_widget_flash(get_ctrllable_widget_pointer());
        set_ctrllable_widget_pointer(ctrllable_num);
        set_ctrllable_widget_flash(get_ctrllable_widget_pointer());
    } else if (ctrllable_num > (PARAM_FLASH_RIGHT_EDGE - PARAM_FLASH_WIDGET_COUNTER4_ONE_LINE) && ctrllable_num <= PARAM_FLASH_RIGHT_EDGE)
    {
        if (ctrllable_num == PARAM_FLASH_RIGHT_EDGE)
        {
            ctrllable_num = PARAM_FLASH_RIGHT_EDGE - PARAM_FLASH_WIDGET_COUNTER4_ONE_LINE;
            clr_ctrllable_widget_flash(get_ctrllable_widget_pointer());
            set_ctrllable_widget_pointer(ctrllable_num);
        }
        ctrllable_num = get_right_ctrllable_widget();
        clr_ctrllable_widget_flash(get_ctrllable_widget_pointer());
        set_ctrllable_widget_pointer(ctrllable_num);
        set_ctrllable_widget_flash(get_ctrllable_widget_pointer());
    }
}
void menu_param_button_down(void)
{
    ctrllable_widget_var_down(get_ctrllable_widget_pointer(), 10);
}
void menu_param_button_up(void)
{
    ctrllable_widget_var_up(get_ctrllable_widget_pointer(), 10);
}
    #endif
#endif
